$(document).ready(function() {	
	$(".notices :button[name='delete']").click(function() {
		var confirmed = confirm("你真的要删除吗?");
		var noticeId = $(this).attr("title");
		if (confirmed) {
			var deleteStatus = deleteNotice(noticeId);
			if(deleteStatus) {
				$(this).parent().parent().remove();
			} else {
				return false;
			}
		} else {
			return false;
		}
	});
	
	$("#modifyDialog").dialog({
		autoOpen : false,
		modal : true,
		buttons : {
			"取消" : function() {
				$(this).dialog("close");
			},
			"提交" : ajaxUpdate
		}
	});
	
	$(".notices :button[name='modify']").click(function() {
		var row = $(this).parent().parent();
		var subtitle = row.children(":eq(1)").text();
		var description = row.children(":eq(2)").text();
		var person = row.children(":eq(3)").text();
		var id = row.children(":eq(0)").text();
		var modifyDialog = $("#modifyDialog");
		modifyDialog.find(":hidden[name='id']").val(id);
		modifyDialog.find(":text[name='subtitle']").val(subtitle);
		modifyDialog.find("textarea[name='description']").val(description);
		modifyDialog.find(":text[name='person']").val(person);
		modifyDialog.dialog("open");
		//将代表修改行的jquery对象放入modifyDialog中,以便ajaxUpdate使用
		modifyDialog.data("modifyRow", row);
	});
	
});

function deleteNotice(noticeId) {
	var deleteStatus = new Boolean(false);
	var ajaxMessage = $(".ajaxMessage");
	$.ajax({
		type : "GET",
		async : true,
		dataType : "json",
		url : "deleteNotice.do?id=" + noticeId,
		success : function(jsonData, textStatus) {
			var response = jsonData;
			var status = response.status;
			var message = response.message;
			
			if (status) {
				deleteStatus = new Boolean(true);				
				ajaxMessage.text("删除成功!").show();
			} else {
				deleteStatus = new Boolean(false);
				ajaxMessage.text("删除失败!" + message).show();
			}			
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			ajaxMessage.text("请求失败...");
			ajaxMessage.show();
			
		}
	});
	setTimeout("$(\".ajaxMessage\").hide()", 3000);
	return deleteStatus;
}

function initialDialog(title, message, modal, buttons, open) {
	var dialog = $("#ajaxDialog");
	dialog.find("p").text(message);
	dialog.dialog("destroy");
	dialog.dialog({
		autoOpen : false,
		modal : modal,
		title : title,
		buttons : buttons
	});
	
	if (open) {
		dialog.dialog("open");
	}
}

function ajaxUpdate() {
	var id = $(this).find(":hidden[name='id']").val();
	var subtitle = $(this).find(":text[name='subtitle']").val();
	var description = $(this).find("textarea").val();
	var person = $(this).find(":text[name='person']").val();
	var notice = {
		"id" : id,
		"subtitle" : subtitle,
		"description" : description,
		"person" : person
	};
	
	$.ajax({
		type : "POST",
		data : notice,
		url : "updateNotice.do",
		async : true,
		dataType : "json",
		success : function(jsonData, textStatus) {
			var response = jsonData;
			var status = response.status;
			
			if (status) {
				var modifyRow = $("#modifyDialog").data("modifyRow");
				modifyRow.children(":eq(1)").text(subtitle);
				modifyRow.children(":eq(2)").text(description);
				modifyRow.children(":eq(3)").text(person);
				$(".ajaxMessage").text("更新成功！");
			} else {
				$(".ajaxMessage").text("更新失败，请稍后再试！");
			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			$(".ajaxMessage").text("请求失败...");	
			
		}
	});
	$(this).dialog("close");
	setTimeout("$(\"#ajaxMessage\").hide()", 4000);
}
